package token

import (
	"encoding/json"
	"github.com/gogf/gf/net/ghttp"
	"strconv"
	"text/app"
	"text/app/service"
	"text/library/in"
	"time"
)

//s, err := app.Redis.Get(token).Result()

//data := new(user.UserInfo)
//if err := json.Unmarshal([]byte(s), data); err != nil {
//in.Json500(r, err)
//}
//app.Redis.Expire(token, time.Duration(time.Hour))
//return data
//获取token
func GetToken(r *ghttp.Request) *service.UserInfo{
	token := r.Header.Get("Authorization")
	s,err:=app.Redis.Get(token).Result()
	if err != nil && err.Error() != "redis: nil" {
		in.Json500(r, err)} else if err != nil {
		in.Json401(r)
	}
	if s=="" {
		in.Json401(r)
	}
	data:=new(service.UserInfo)
	if err:=json.Unmarshal([]byte(s),&data);err!=nil {
		in.Json500(r,err)
	}
	return data
}

//生成唯一键值
func  RedisKey(Name string) string {
	key:=Name+strconv.FormatInt(time.Now().Unix(),10)//用户姓名+redis创建的时间合成key值
	return  key
}

